package com.qf.spring22022.controller;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadController {
    @Value("${myfile.dir}")
    private String fileDir;

    @PostMapping("/test01")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        String filename = UUID.randomUUID().toString();
        String originalFilename = file.getOriginalFilename();
        filename = filename +"."+ FilenameUtils.getExtension(originalFilename);
        String finalFilePath = fileDir + File.separator + filename;
        file.transferTo(new File(finalFilePath));
        return "ok";
    }
}
